package edu.depaul.se.courseregistration.jpa.user;

import edu.depaul.se.courseregistration.jpa.Address;
import edu.depaul.se.courseregistration.jpa.schedule.Schedule;
import java.io.Serializable;
import java.util.List;
import javax.persistence.*;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlSeeAlso;

@Entity
@Table(name = "PERSON")
@Inheritance(strategy= InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name="USR_TYPE")
@NamedQueries({
    @NamedQuery(name=Person.PERSON_QUERY_ALL,query="select p from Person p"), //Find all Users    
    @NamedQuery(name = Person.PERSON_QUERY_BY_ID, query = "SELECT p FROM Person p WHERE p.personID = :" + Person.PERSON_ID_PARAMETER_NAME), //Find Person By ID.
    @NamedQuery(name = Person.PERSON_QUERY_BY_FIRSTNAME, query = "SELECT p from Person p WHERE p.firstName = :" + Person.PERSON_FIRSTNAME_PARAMETER_NAME), //Find Person by FirstName
    @NamedQuery(name = Person.PERSON_QUERY_BY_LASTNAME, query = "SELECT p from Person p WHERE p.lastName = :" + Person.PERSON_LASTNAME_PARAMETER_NAME), //Find Person by LastName
    @NamedQuery(name = Person.PERSON_QUERY_BY_EMAIL, query = "SELECT p from Person p WHERE p.email = :" + Person.PERSON_EMAIL_PARAMETER_NAME) //Find Person by Email        
})
//@XmlRootElement
@XmlSeeAlso({DBAdmin.class,Professor.class,Registrar.class,Student.class})
public abstract class Person implements Serializable {
    private static final long serialVersionUID = 1L;
    
    public static final String PERSON_QUERY_ALL = "Person.findAll";
    public static final String PERSON_QUERY_BY_ID = "Person.findById";
    public static final String PERSON_QUERY_BY_FIRSTNAME = "Person.findByFirstName";
    public static final String PERSON_QUERY_BY_LASTNAME = "Person.findByLastName";
    public static final String PERSON_QUERY_BY_EMAIL = "Person.findByEmail";
    public static final String PERSON_ID_PARAMETER_NAME = "personID";
    public static final String PERSON_FIRSTNAME_PARAMETER_NAME = "firstName";
    public static final String PERSON_LASTNAME_PARAMETER_NAME = "lastName";
    public static final String PERSON_EMAIL_PARAMETER_NAME = "email";
    
    
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "PERSON_ID")
    private Integer personID;
    @Column(name = "FIRST_NAME")
    private String firstName;
    @Column(name = "LAST_NAME")
    private String lastName;
    @Column(name = "EMAIL")
    private String email;
    @Column(name = "DEPARTMENT_NAME")
    private String departmentName;
    
    @OneToMany(cascade = CascadeType.ALL,
               mappedBy = "personID",
               fetch = FetchType.EAGER)
   // @OrderBy("sectionId DSC")
    
    private List<Schedule> schedule;
    @XmlElement(name="schedule")
    public List<Schedule> getSchedule() {
        return schedule;
    }

    public void setSchedule(List<Schedule> schedule) {
        this.schedule = schedule;
    }
    
    @Embedded
    private Address address;
    
    public String getDepartmentName() {
        return departmentName;
    }

    public void setDepartmentName(String departmentName) {
        this.departmentName = departmentName;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    } 
    
    public Address getAddress() {
        return address;
    }

    public void setAddress(Address address) {
        this.address = address;
    }

    public Integer getPersonID() {
        return personID;
    }

    public void setPersonID(Integer personID) {
        this.personID = personID;
    }
    
        @Override
    public int hashCode() {
        int hash = 0;
        hash += (personID != null ? personID.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        if (!(object instanceof Person)) {
            return false;
        }
        Person other = (Person) object;
        if ((this.personID == null && other.personID != null) || (this.personID != null && !this.personID.equals(other.personID))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append("Person [ personId=");
        sb.append(personID);
         sb.append(firstName).append(" ").append(lastName);
        sb.append(",  email=[");
        sb.append(email);
        return sb.toString();
    }
    
}
